package com.itheima.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.HomeAdvertise;
import com.itheima.mapper.HomeAdvertiseMapper;
import com.itheima.service.IHomeAdvertiseService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 首页轮播广告表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class HomeAdvertiseServiceImpl extends ServiceImpl<HomeAdvertiseMapper, HomeAdvertise> implements IHomeAdvertiseService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    HomeAdvertiseMapper homeAdvertiseMapper;


    @Override
    public PageBeanVo findByPage(Integer pageSize, Integer pageNum, Integer type, String endTime, String name) {
        LambdaQueryWrapper<HomeAdvertise>lqw=new LambdaQueryWrapper<>();
        Page<HomeAdvertise>page=new Page<>(pageNum,pageSize);

        lqw.eq(type!=null,HomeAdvertise::getType,type);
        lqw.like(name!=null,HomeAdvertise::getName,name);
        lqw.like(endTime!=null,HomeAdvertise::getEndTime,endTime);
        lqw.orderByDesc(HomeAdvertise::getSort);
        homeAdvertiseMapper.selectPage(page,lqw);
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    @Override
    public void addNew(HomeAdvertise homeAdvertise) {

        String url = stringRedisTemplate.opsForValue().get("url");
        homeAdvertise.setUrl(url);
        homeAdvertiseMapper.insert(homeAdvertise);

        stringRedisTemplate.delete("url");

    }

    @Override
    public void delete(Integer[] ids) {
        if(ArrayUtil.isNotEmpty(ids))
        {
            for (Integer id : ids) {
                homeAdvertiseMapper.deleteById(id);

            }
        }
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        HomeAdvertise homeAdvertise = homeAdvertiseMapper.selectById(id);
        homeAdvertise.setStatus(status);
        homeAdvertiseMapper.updateById(homeAdvertise);
    }

    @Override
    public void updateAdvertise(HomeAdvertise advertise, Integer id) {
        HomeAdvertise homeAdvertise = homeAdvertiseMapper.selectById(id);

        String url = stringRedisTemplate.opsForValue().get("url");
        homeAdvertise.setUrl(url);
        homeAdvertise.setId(id.longValue());
        homeAdvertiseMapper.updateById(homeAdvertise);

        stringRedisTemplate.delete("url");
    }

    @Override
    public HomeAdvertise getAdvertise(Integer id) {
        HomeAdvertise homeAdvertise = homeAdvertiseMapper.selectById(id);
        return homeAdvertise;
    }
}
